package com.cmcm.network;

import org.aspectj.bridge.IMessage;
import org.aspectj.bridge.MessageHandler;
import org.aspectj.tools.ajc.Main;
import org.gradle.api.Project;
import org.gradle.api.logging.Logger;

import java.io.File;

/**
 * Created by gezihua on 17-1-4.
 */

public class JarAspectHandler extends NetHandler {
    public String getTargetCompatibility() {
        return targetCompatibility;
    }

    public void setTargetCompatibility(String targetCompatibility) {
        this.targetCompatibility = targetCompatibility;
    }

    public String getSourceCompatibility() {
        return sourceCompatibility;
    }

    public void setSourceCompatibility(String sourceCompatibility) {
        this.sourceCompatibility = sourceCompatibility;
    }

    public String getBootClassPath() {
        return bootClassPath;
    }

    public void setBootClassPath(String bootClassPath) {
        this.bootClassPath = bootClassPath;
    }

    private String sourceCompatibility;
    private String targetCompatibility;
    private String bootClassPath;

    public JarAspectHandler(Project project) {
        super(project);
    }

    public void setAjPath(String ajPath) {
        mAjPath = ajPath;
    }

    private String mAjPath;

    public   void handle() {
        String[] args = {
                "-showWeaveInfo",
                "-encoding", getEncoding(),
                "-source", sourceCompatibility,
                "-target", targetCompatibility,
                "-d", getOutPath(),
                "-classpath", getClassPath()+(File.separator),
                "-bootclasspath", bootClassPath
        };
        MessageHandler handler = new MessageHandler(true);
        Logger log = getLogger();
        log.error("start compile");
        new Main().run(args, handler);
        for (IMessage message : handler.getMessages(null, true)) {
            log(message);
        }
    }

    public String getAjPath() {
        return mAjPath;
    }
}
